# Copyright (C) 2014-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test support for D demangling.  The tests here intentionally do not
# require a D compiler, it simply calls the internal demangler with
# synthesized strings and tests the results.

load_lib "d-support.exp"

if { [skip_d_tests] } { continue }

### Utility function for test_demangling and test_demangling_exact.
proc test_demangling {test result} {
    gdb_test_exact "demangle $test" $result $test
}

proc test_d_demangling {} {

    #  Test D demangler
    test_demangling "_Dmain" "D main"

    test_demangling "_D8demangle4testFaZv" "demangle.test(char)"
    test_demangling "_D8demangle4testFbZv" "demangle.test(bool)"
    test_demangling "_D8demangle4testFcZv" "demangle.test(creal)"
    test_demangling "_D8demangle4testFdZv" "demangle.test(double)"
    test_demangling "_D8demangle4testFeZv" "demangle.test(real)"
    test_demangling "_D8demangle4testFfZv" "demangle.test(float)"
    test_demangling "_D8demangle4testFgZv" "demangle.test(byte)"
    test_demangling "_D8demangle4testFhZv" "demangle.test(ubyte)"
    test_demangling "_D8demangle4testFiZv" "demangle.test(int)"
    test_demangling "_D8demangle4testFjZv" "demangle.test(ireal)"
    test_demangling "_D8demangle4testFkZv" "demangle.test(uint)"
    test_demangling "_D8demangle4testFlZv" "demangle.test(long)"
    test_demangling "_D8demangle4testFmZv" "demangle.test(ulong)"
    test_demangling "_D8demangle4testFnZv" "demangle.test(none)"
    test_demangling "_D8demangle4testFoZv" "demangle.test(ifloat)"
    test_demangling "_D8demangle4testFpZv" "demangle.test(idouble)"
    test_demangling "_D8demangle4testFqZv" "demangle.test(cfloat)"
    test_demangling "_D8demangle4testFrZv" "demangle.test(cdouble)"
    test_demangling "_D8demangle4testFsZv" "demangle.test(short)"
    test_demangling "_D8demangle4testFtZv" "demangle.test(ushort)"
    test_demangling "_D8demangle4testFuZv" "demangle.test(wchar)"
    test_demangling "_D8demangle4testFvZv" "demangle.test(void)"
    test_demangling "_D8demangle4testFwZv" "demangle.test(dchar)"

    test_demangling "_D8demangle4testFOaZv" "demangle.test(shared(char))"
    test_demangling "_D8demangle4testFxaZv" "demangle.test(const(char))"
    test_demangling "_D8demangle4testFyaZv" "demangle.test(immutable(char))"
    test_demangling "_D8demangle4testFNgaZv" "demangle.test(inout(char))"
    test_demangling "_D8demangle4testFOxaZv" "demangle.test(shared(const(char)))"
    test_demangling "_D8demangle4testFONgaZv" "demangle.test(shared(inout(char)))"
    test_demangling "_D8demangle4testFAaZv" "demangle.test(char\[\])"
    test_demangling "_D8demangle4testFAAaZv" "demangle.test(char\[\]\[\])"
    test_demangling "_D8demangle4testFAAAaZv" "demangle.test(char\[\]\[\]\[\])"
    test_demangling "_D8demangle4testFG42aZv" "demangle.test(char\[42\])"
    test_demangling "_D8demangle4testFG42G42aZv" "demangle.test(char\[42\]\[42\])"
    test_demangling "_D8demangle4testFG42G42G42aZv" "demangle.test(char\[42\]\[42\]\[42\])"
    test_demangling "_D8demangle4testFG1234567890aZv" "demangle.test(char\[1234567890\])"
    test_demangling "_D8demangle4testFHaaZv" "demangle.test(char\[char\])"
    test_demangling "_D8demangle4testFHHaaaZv" "demangle.test(char\[char\[char\]\])"
    test_demangling "_D8demangle4testFPaZv" "demangle.test(char*)"
    test_demangling "_D8demangle4testFPPaZv" "demangle.test(char**)"
    test_demangling "_D8demangle4testFPPPaZv" "demangle.test(char***)"

    test_demangling "_D8demangle4testFNhG8gZv" "demangle.test(__vector(byte\[8\]))"
    test_demangling "_D8demangle4testFNhG16gZv" "demangle.test(__vector(byte\[16\]))"
    test_demangling "_D8demangle4testFNhG32gZv" "demangle.test(__vector(byte\[32\]))"
    test_demangling "_D8demangle4testFNhG4sZv" "demangle.test(__vector(short\[4\]))"
    test_demangling "_D8demangle4testFNhG8sZv" "demangle.test(__vector(short\[8\]))"
    test_demangling "_D8demangle4testFNhG16sZv" "demangle.test(__vector(short\[16\]))"
    test_demangling "_D8demangle4testFNhG2iZv" "demangle.test(__vector(int\[2\]))"
    test_demangling "_D8demangle4testFNhG4iZv" "demangle.test(__vector(int\[4\]))"
    test_demangling "_D8demangle4testFNhG8iZv" "demangle.test(__vector(int\[8\]))"
    test_demangling "_D8demangle4testFNhG1lZv" "demangle.test(__vector(long\[1\]))"
    test_demangling "_D8demangle4testFNhG2lZv" "demangle.test(__vector(long\[2\]))"
    test_demangling "_D8demangle4testFNhG4lZv" "demangle.test(__vector(long\[4\]))"
    test_demangling "_D8demangle4testFNhG2fZv" "demangle.test(__vector(float\[2\]))"
    test_demangling "_D8demangle4testFNhG4fZv" "demangle.test(__vector(float\[4\]))"
    test_demangling "_D8demangle4testFNhG8fZv" "demangle.test(__vector(float\[8\]))"
    test_demangling "_D8demangle4testFNhG1dZv" "demangle.test(__vector(double\[1\]))"
    test_demangling "_D8demangle4testFNhG2dZv" "demangle.test(__vector(double\[2\]))"
    test_demangling "_D8demangle4testFNhG4dZv" "demangle.test(__vector(double\[4\]))"

    test_demangling "_D8demangle4testFI5identZv" "demangle.test(ident)"
    test_demangling "_D8demangle4testFI5ident4testZv" "demangle.test(ident.test)"
    test_demangling "_D8demangle4testFC5classZv" "demangle.test(class)"
    test_demangling "_D8demangle4testFC5class4testZv" "demangle.test(class.test)"
    test_demangling "_D8demangle4testFS6structZv" "demangle.test(struct)"
    test_demangling "_D8demangle4testFS6struct4testZv" "demangle.test(struct.test)"
    test_demangling "_D8demangle4testFE4enumZv" "demangle.test(enum)"
    test_demangling "_D8demangle4testFE4enum4testZv" "demangle.test(enum.test)"
    test_demangling "_D8demangle4testFT7typedefZv" "demangle.test(typedef)"
    test_demangling "_D8demangle4testFT7typedef4testZv" "demangle.test(typedef.test)"

    test_demangling "_D8demangle4testFJaZv" "demangle.test(out char)"
    test_demangling "_D8demangle4testFKaZv" "demangle.test(ref char)"
    test_demangling "_D8demangle4testFLaZv" "demangle.test(lazy char)"
    test_demangling "_D8demangle4testFMaZv" "demangle.test(scope char)"
    test_demangling "_D8demangle4testFaXv" "demangle.test(char...)"
    test_demangling "_D8demangle4testFaYv" "demangle.test(char, ...)"
    test_demangling "_D8demangle4testFaaYv" "demangle.test(char, char, ...)"
    test_demangling "_D8demangle4testFaaZv" "demangle.test(char, char)"

    test_demangling "_D8demangle4testFDFZaZv" "demangle.test(char() delegate)"
    test_demangling "_D8demangle4testFDUZaZv" "demangle.test(extern(C) char() delegate)"
    test_demangling "_D8demangle4testFDWZaZv" "demangle.test(extern(Windows) char() delegate)"
    test_demangling "_D8demangle4testFDVZaZv" "demangle.test(extern(Pascal) char() delegate)"
    test_demangling "_D8demangle4testFDRZaZv" "demangle.test(extern(C++) char() delegate)"

    test_demangling "_D8demangle4testFFZaZv" "demangle.test(char() function)"
    test_demangling "_D8demangle4testFUZaZv" "demangle.test(extern(C) char() function)"
    test_demangling "_D8demangle4testFWZaZv" "demangle.test(extern(Windows) char() function)"
    test_demangling "_D8demangle4testFVZaZv" "demangle.test(extern(Pascal) char() function)"
    test_demangling "_D8demangle4testFRZaZv" "demangle.test(extern(C++) char() function)"

    test_demangling "_D8demangle4testFDFNaZaZv" "demangle.test(char() pure delegate)"
    test_demangling "_D8demangle4testFDFNbZaZv" "demangle.test(char() nothrow delegate)"
    test_demangling "_D8demangle4testFDFNcZaZv" "demangle.test(char() ref delegate)"
    test_demangling "_D8demangle4testFDFNdZaZv" "demangle.test(char() @property delegate)"
    test_demangling "_D8demangle4testFDFNeZaZv" "demangle.test(char() @trusted delegate)"
    test_demangling "_D8demangle4testFDFNfZaZv" "demangle.test(char() @safe delegate)"

    test_demangling "_D8demangle4testFFNaZaZv" "demangle.test(char() pure function)"
    test_demangling "_D8demangle4testFFNbZaZv" "demangle.test(char() nothrow function)"
    test_demangling "_D8demangle4testFFNcZaZv" "demangle.test(char() ref function)"
    test_demangling "_D8demangle4testFFNdZaZv" "demangle.test(char() @property function)"
    test_demangling "_D8demangle4testFFNeZaZv" "demangle.test(char() @trusted function)"
    test_demangling "_D8demangle4testFFNfZaZv" "demangle.test(char() @safe function)"
    test_demangling "_D8demangle4testFFNaNbZaZv" "demangle.test(char() pure nothrow function)"
    test_demangling "_D8demangle4testFFNbNaZaZv" "demangle.test(char() nothrow pure function)"
    test_demangling "_D8demangle4testFFNdNfNaZaZv" "demangle.test(char() @property @safe pure function)"

    test_demangling "_D8demangle4test6__vtblZ" "vtable for demangle.test"
    test_demangling "_D8demangle4test6__initZ" "demangle.test.init$"
    test_demangling "_D8demangle4test12__ModuleInfoZ" "ModuleInfo for demangle.test"
    test_demangling "_D8demangle4test7__ClassZ" "ClassInfo for demangle.test"
    test_demangling "_D8demangle4test11__InterfaceZ" "Interface for demangle.test"

    test_demangling "_D8demangle4test6__ctorMFZv" "demangle.test.this()"
    test_demangling "_D8demangle4test6__dtorMFZv" "demangle.test.~this()"
    test_demangling "_D8demangle4test10__postblitMFZv" "demangle.test.this(this)"

    test_demangling "_D8demangle4testFHAbaZv" "demangle.test(char\[bool\[\]\])"
    test_demangling "_D8demangle4testFHG42caZv" "demangle.test(char\[creal\[42\]\])"
    test_demangling "_D8demangle4testFAiXv" "demangle.test(int\[\]...)"
    test_demangling "_D8demangle4testFLAiXv" "demangle.test(lazy int\[\]...)"
    test_demangling "_D8demangle4testFAiYv" "demangle.test(int\[\], ...)"
    test_demangling "_D8demangle4testFLAiYv" "demangle.test(lazy int\[\], ...)"
    test_demangling "_D8demangle4testFLilZv" "demangle.test(lazy int, long)"
    test_demangling "_D8demangle4testFLliZv" "demangle.test(lazy long, int)"

    test_demangling "_D8demangle4testPFLAiYi" "demangle.test"
    test_demangling "_D1a1bi" "a.b"
    test_demangling "_D1a1bPFiZi" "a.b"
    test_demangling "_D4test3fooAa" "test.foo"
    test_demangling "_D4test2dgDFiYd" "test.dg"
    test_demangling "_D8demangle8demangleFAaZAa" "demangle.demangle(char\[\])"
    test_demangling "_D6object6Object8opAssignFC6ObjectZi" "object.Object.opAssign(Object)"
    test_demangling "_D6object6Object8opEqualsFC6ObjectZi" "object.Object.opEquals(Object)"
    test_demangling "_D8demangle4testFLC6ObjectLDFLiZiZi" "demangle.test(lazy Object, lazy int(lazy int) delegate)"
    test_demangling "_D6plugin8generateFiiZAya" "plugin.generate(int, int)"
    test_demangling "_D8demangle3fnAFZ3fnBMFZv" "demangle.fnA().fnB()"
    test_demangling "_D8demangle4mainFZ1S3fnCMFZv" "demangle.main().S.fnC()"
    test_demangling "_D8demangle4mainFZ1S3fnDMFZv" "demangle.main().S.fnD()"
    test_demangling "_D8demangle2fnFNgiZNgi" "demangle.fn(inout(int))"
    test_demangling "_D8demangle4mainFZ5localMFZi" "demangle.main().local()"
    test_demangling "_D3std5ascii9uppercaseyAa" "std.ascii.uppercase"
    test_demangling "_D3std6stream9BOMEndianyG5E3std6system6Endian" "std.stream.BOMEndian"
    test_demangling "_D3std8internal7uni_tab10unicodeNkoyS3std8internal3uni12CodepointSet" "std.internal.uni_tab.unicodeNko"
    test_demangling "_D2gc2gc2GC6addrOfMFPvZPv" "gc.gc.GC.addrOf(void*)"
    test_demangling "_D3std7process10setCLOEXECFibZv" "std.process.setCLOEXEC(int, bool)"
    test_demangling "_D3std6digest2md3MD53putMFNaNbNeMAxhXv" "std.digest.md.MD5.put(scope const(ubyte)\[\]...)"
    test_demangling "_D3std6mmfile6MmFile13opIndexAssignMFhmZh" "std.mmfile.MmFile.opIndexAssign(ubyte, ulong)"
    test_demangling "_D3std7process18escapeShellCommandFxAAaXAya" "std.process.escapeShellCommand(const(char\[\]\[\])...)"
    test_demangling "_D4core4sync5mutex5Mutex6__ctorMFC6ObjectZC4core4sync5mutex5Mutex" "core.sync.mutex.Mutex.this(Object)"
    test_demangling "_D6object14TypeInfo_Array8argTypesMFNbNfJC8TypeInfoJC8TypeInfoZi" "object.TypeInfo_Array.argTypes(out TypeInfo, out TypeInfo)"
    test_demangling "_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ7tryExecMFMDFZvZv" "rt.dmain2._d_run_main(int, char**, extern(C) int(char\[\]\[\]) function*).tryExec(scope void() delegate)"
    test_demangling "_D6object9Exception6__ctorMFNaNbNfAyaAyamC6object9ThrowableZC9Exception" "object.Exception.this(immutable(char)\[\], immutable(char)\[\], ulong, object.Throwable)"
    test_demangling "_D3gcc3deh17parse_lsda_headerFPS3gcc6unwind7generic15_Unwind_ContextPhPS3gcc3deh16lsda_header_infoZPh" "gcc.deh.parse_lsda_header(gcc.unwind.generic._Unwind_Context*, ubyte*, gcc.deh.lsda_header_info*)"
    test_demangling "_D3std6socket23UnknownAddressReference6__ctorMFPS4core3sys5posix3sys6socket8sockaddrkZC3std6socket23UnknownAddressReference" "std.socket.UnknownAddressReference.this(core.sys.posix.sys.socket.sockaddr*, uint)"
}

# Start with a fresh gdb.

gdb_exit
gdb_start

if [set_lang_d] {
    gdb_test_no_output "set width 0"

    test_d_demangling

    # Verify we can specify the d language to demangle.
    gdb_test_exact "demangle -l d -- _Dmain" "D main"
} else {
    warning "D demangling tests suppressed."
}
